import { defineStore } from 'pinia'
import { requestLyric } from "@/api/index"
//引入home仓库请求到的数据直接使用,免得再请求一次
import { homeStore } from "@/store/home.js"
//注意,defineStore的名字必须是唯一的
export const musicplaycontrolStore = defineStore('musicplaycontrol', {
  state: () => {
    return {
      //播放列表
      // playlist: homeStore().songlistdetail.tracks,
      playCurrentIndex: 0,
      // 播放状态
      pause: false,
      // 歌词,数字来home模块下的单个歌曲信息
      lyric: '',
    }
  },
  actions: {
    setplaylist(value) {
      this.playlist = value
    },
    // 获取歌词信息
    async asyncrequestLyric(id) {
      let res = await requestLyric(id)
      // console.log('里面', res);
      if (res.data.lrc) {
        this.lyric = res.data.lrc.lyric || []
        // console.log('歌词', res);
        return 'ok'
      } else {
        console.log('请求歌词失败');
      }
    }
  },
  getters: {
    playlist() {
      return homeStore().songlistdetail.tracks
    },
    lyricsList: function (state) {
      let arr = state.lyric.split(/\n/igs).map((item, index) => {
        let min = item.slice(1, 3);
        let sec = item.slice(4, 6);
        let mill = item.slice(7, 10);
        return {
          min, sec, mill, lyric: item.slice(11, item.length),
          time: parseInt(mill) + parseInt(sec) * 1000 + parseInt(min) * 60 * 1000
        }
      })
      console.log(arr);
      return arr
    }
  }
})
